package com.example.comparable;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

// 1 实现writable接口
public class FlowBean2 implements WritableComparable<FlowBean2> {
    private long upFlow;
    private long downFlow;
    private long sumFlow;
    //2  反序列化时，需要反射调用空参构造函数，所以必须有
    public FlowBean2(){
        super();
    }
    public FlowBean2(long upFlow, long downFlow) {
        super();
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = upFlow + downFlow;
    }
    //3  序列化方法
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(upFlow);
        out.writeLong(downFlow);
        out.writeLong(sumFlow);
    }
    //4 反序列化方法
    //5 反序列化方法读顺序必须和写序列化方法的写顺序必须一致
    @Override
    public void readFields(DataInput in) throws IOException {
        this.upFlow  = in.readLong();
        this.downFlow = in.readLong();
        this.sumFlow = in.readLong();
    }

    public long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public long getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }

    @Override
    public String toString() {
        return upFlow + "\t" + downFlow + "\t" + sumFlow;
    }



    @Override
    public int compareTo(FlowBean2 o) {
        //按照总流量排序：倒序
        int result ;
        if (this.sumFlow>o.getSumFlow()){
            result=-1;
        }else if (this.sumFlow<o.getSumFlow()){
            result=1;
        }else {
//            //当总流量形同时，按照上行流量正序排序
//            if (upFlow > o.getUpFlow()){
//                result =1;
//            }else if (upFlow < o.getUpFlow()){
//                result=-1;
//            }else {
//                result =0;
//            }
            result =0;
        }
        return result;
    }
}